package redora.rdo.configuration.rdo.service;

import java.util.List;
import java.util.Set;
import java.util.Vector;

import redora.rdo.api.BusinessRuleViolation;
import redora.rdo.api.FetchScope;
import redora.rdo.configuration.rdo.model.RedoraTrash;
import redora.rdo.exceptions.BusinessRuleViolationException;
import redora.rdo.exceptions.ConnectException;
import redora.rdo.exceptions.CopyException;
import redora.rdo.exceptions.PersistException;
import redora.rdo.exceptions.QueryException;

public class TrashService {
	/**
	 * Create a new record in the RedoraTrash table. You still have to set roDeleted to true
	 * in the trashed object.
	 * 
	 * @param undoHash (Optional) When null, RedoraTrash will calculate a new hash.
	 * @param objectId (Mandatory) Id of the trashed object
	 * @param objectName (Mandatory) Name of the object
	 * @throws PersistException
	 *    When a business rule is violated an exception is thrown
	 *    because this should be unexpected.
	 */
	public static void trash(String undoHash, Long objectId, String objectName)
		throws ConnectException, PersistException, BusinessRuleViolationException, QueryException {
		RedoraTrash trash = new RedoraTrash();
		if (undoHash != null) {
			trash.setUndoHash(undoHash);
		}
		trash.setObjectId(objectId);
		trash.setObjectName(objectName);
		Set<BusinessRuleViolation> ret = ServiceFactory.redoraTrashService()
				.persist(trash);
		if (!ret.isEmpty()) {
			throw new PersistException(
					"Unexpected business rule violation(s). " + ret.size()
							+ " rules have been violated, the first rule is "
							+ ret.iterator().next().getBusinessRuleId());
		}

		if (undoHash == null) {
			undoHash = trash.getUndoHash();
		}
	}

	/**
	 * Deletes the RedoraTrash. It will NOT delete the associated trashed
	 * records.
	 * 
	 * @param objectId  (Mandatory)
	 * @param objectName (Mandatory)
	 * @throws PersistException
	 *    When a business rule is violated an exception is thrown
	 *    because this should be unexpected.
	 */
	public static void deleteTrash(Long objectId, String objectName)
			throws ConnectException, PersistException,
			BusinessRuleViolationException, QueryException, CopyException {
		List<Object> params = new Vector<Object>();
		params.add(objectId);
		params.add(objectName);
		List<RedoraTrash> trashList = ServiceFactory.redoraTrashService()
				.find(RedoraTrashSQL.FIND_BY_NAME_AND_ID, params, FetchScope.List);
		if (trashList.size() != 1) {
			throw new QueryException(
					"I expected to find one RedOrangeTrash, but i found: "
							+ trashList.size());
		}

		Set<BusinessRuleViolation> ret = ServiceFactory.redoraTrashService()
				.delete(trashList.iterator().next());
		if (!ret.isEmpty()) {
			throw new PersistException(
					"Unexpected business rule violation(s). " + ret.size()
							+ " rules have been violated, the first rule is "
							+ ret.iterator().next().getBusinessRuleId());
		}
	}
}
